using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CHEnhanceEfCore.DataBase;
using CHEnhanceEfCore.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Internal;

namespace CHEnhanceEfCore.Service.impl
{
    /// <summary>
    /// author: chenhao
    /// </summary>
    public class RoleServiceImpl : IRoleService
    {
        private readonly ChEnhanceDbContext _dbContext;

        public RoleServiceImpl(ChEnhanceDbContext dbContext)
        {
            _dbContext = dbContext;
        }

        /// <summary>
        /// 查询用户的权限code
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public async Task<List<string>> getRoleCodesByUserIdAsync(Guid userId)
        {
            //避坑 切勿使用LeftJoin
            List<string> codeList = await _dbContext.userRoleSet.Where(ur => ur.userId == userId)
                .Join(_dbContext.roleSet, ur => ur.roleId,
                    r => r.roleId, (ur, r) => r.code).Select(c => c)
                .ToListAsync();
            return codeList;
        }
    }
}